{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MobileCoin Example Wallet\n",
    "\n",
    "This is an example python client that interacts with `mobilecoind` to manage a MobileCoin wallet.\n",
    "\n",
    "You must start the `mobilecoind` daemon in order to run a wallet. See the mobilecoind README for more information.\n",
    "\n",
    "To run this notebook, make sure you have the requirements installed, and that you have compiled the grpc protos.\n",
    "\n",
    "```\n",
    "cd mobilecoind/clients/python/jupyter\n",
    "./install.sh\n",
    "jupyter notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mobilecoin import Client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Start the Mob Client\n",
    "\n",
    "The client talks to your local mobilecoind. See the mobilecoind/README.md for information on how to set it up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = Client(\"localhost:4444\", ssl=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Input Root Entropy for Account\n",
    "\n",
    "Note: The root entropy is sensitive material. It is used as the seed to create your account keys. Anyone with your root entropy can steal your MobileCoin."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "entropy = \"4ec2c081e764f4189afba528956c05804a448f55f24cc3d04c9ef7e807a93bcd\"\n",
    "credentials_response = client.get_account_key(bytes.fromhex(entropy))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Monitor your Account\n",
    "\n",
    "Monitoring an account means that mobilecoind will persist the transactions that belong to you to a local database. This allows you to retrieve your funds and calculate your balance, as well as to construct and submit transactions.\n",
    "\n",
    "Note: MobileCoin uses accounts and subaddresses for managing funds. You can optionally specify a range of subaddresses to monitor. See mob_client.py for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "monitor_id_response = client.add_monitor(credentials_response.account_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Check Balance\n",
    "\n",
    "You will need to provide a subaddress index. Most people will only use one subaddress, and can default to 0. Exchanges or users who want to generate lots of new public addresses may use multiple subaddresses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "subaddress_index = 0\n",
    "client.get_balance(monitor_id_response.monitor_id, subaddress_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Send a Transaction\n",
    "\n",
    "MobileCoin uses \"request codes\" to wrap public addresses. See below for how to generate request codes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_code = \"2nTy8m2VE5UMtfqRf12gEjZmFHKNTDEtNufQZNvE713ytYvdu2kqpbcncHJUSLwmgTCkB56Li9fsGwJF9LRYEQvoQCDzqVQEJETDNQKLzqHCzd\"\n",
    "target_address_response = client.parse_request_code(address_code)\n",
    "\n",
    "# Construct the transaction\n",
    "txo_list_response = client.get_unspent_tx_output_list(monitor_id_response.monitor_id, subaddress_index)\n",
    "outlays = [{\n",
    "    'value': 10, \n",
    "    'receiver': target_address_response.receiver\n",
    "}]\n",
    "tx_proposal_response = client.generate_tx(\n",
    "    monitor_id_response.monitor_id, \n",
    "    subaddress_index, \n",
    "    txo_list_response.output_list, \n",
    "    outlays\n",
    ")\n",
    "\n",
    "# Send the transaction to consensus validators\n",
    "client.submit_tx(tx_proposal_response.tx_proposal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Public Address (Request Code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public_address_response = client.get_public_address(monitor_id_response.monitor_id, subaddress_index)\n",
    "request_code_response = client.create_request_code(public_address_response.public_address)\n",
    "print(f\"Request code = {request_code_response}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
